/*-
 * ThirdPartProcessor.java
 *
 * Date: 2017年3月27日
 * Time: 上午10:47:03
 *
 * 版权所有©2012-2013 rengh
 */

package com.lmx.thirdPart;

import java.io.IOException;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.UserService;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.HealthInfo;
import com.lmx.repoDomain.tmodel.Message;

import net.sf.json.JSONObject;

/**
 * #{description}
 * 
 * @author lmx
 * @version 1.00
 */

public abstract class ThirdPartProcessor implements BusinessProcessor {
    
    @Autowired
    protected UserService userService;
    
    protected CloseableHttpClient client = null;
    
    protected PoolingHttpClientConnectionManager cm=null;
    
    //post方式获取数据
    public String post(String url, Map<String, String> header, String argument)
            throws ClientProtocolException, IOException,ThirdPartBusiException {
        HttpPost post = new HttpPost(url);
        for (Map.Entry<String, String> element : header.entrySet()) {
            post.addHeader(element.getKey(), element.getValue());
        }
        StringEntity entityPara = new StringEntity(argument, "utf-8");// 解决中文乱码问题
        post.setEntity(entityPara);
        CloseableHttpResponse response = client.execute(post);
        String responseData=EntityUtils.toString(response.getEntity());
        response.close();
        return responseData;
    }
    //get方式获取数据
    public String get(String url, Map<String, String> header, String argument)
            throws ClientProtocolException, IOException,ThirdPartBusiException {
        HttpGet get = new HttpGet(url);
        for (Map.Entry<String, String> element : header.entrySet()) {
            get.addHeader(element.getKey(), element.getValue());
        }
        CloseableHttpResponse response = client.execute(get);
        String responseData=EntityUtils.toString(response.getEntity());
        response.close();
        return responseData;
    }

    /*
     * 根据健康数据信息，生成提醒数据
     */
    @Transactional
    public Message buildHealthMessage(Customer customer,HealthInfo info){
        Message message = new Message();
        message.setDelStatus((short)0);
		message.setTenant(customer.getTenant());
        message.setType(new Integer(CommonConstants.MESSAGE.MESSAGETYPE_NOTICE.label()).shortValue());
        message.setCode(CommonConstants.MESSAGE.MESSAGECODE_HEALTHNOTICE.label());
        message.setCreateTime(CommonUtil.getCurrTimes());
        //使用健康数据类型作为通知的业务类型
        message.setBusiness(info.getItemtype().getTypecode());
        //拼装内容信息
        JSONObject content = new JSONObject();
        content.put("createTime", info.getCreateDateTime());
        content.put("customerId", customer.getId());
        content.put("customerName", customer.getName());
		content.put("result", info.getResult());
		content.put("resultAddtion", info.getResultAddtion());
		content.put("resultAddtionSec", info.getResultAddtionSec());
		content.put("resultAddtionThir",info.getResultAddtionThir());
		content.put("resultAddtionFou", info.getResultAddtionFou());
		content.put("resultAddtionFiv", info.getResultAddtionFiv());
		content.put("resultAddtionSix", info.getResultAddtionSix());
		content.put("resultAddtionSev", info.getResultAddtionSec());
		content.put("resultAddtionEig", info.getResultAddtionEig());
		content.put("resultAddtionNin", info.getResultAddtionNin());
		content.put("resultAddtionTen", info.getResultAddtionTen());
        message.setContent(content.toString());
        //其他信息
        message.setBusiness(info.getItemtype().getTypecode());
        message.setHasRead(new Integer(0).shortValue());
        //取得需要发送的用户--发送给所有本机构的人员
        message.setUserList(userService.getAllUserList(customer.getTenant()));
        return message;
    }
    
}
